package com.bootdo.system.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.dao.UserRoleDao;
import com.bootdo.system.domain.GuessItemDO;
import com.bootdo.system.domain.GuessLogDO;
import com.bootdo.system.service.GuessItemService;
import com.bootdo.system.service.GuessLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.GuessDO;
import com.bootdo.system.service.GuessService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author huizhang
 * @email 
 * @date 2021-04-09 13:43:45
 */
 
@Controller
@RequestMapping("/system/guess")
public class GuessController extends BaseController {

	@Autowired
	private UserRoleDao userRoleDao;

	@Autowired
	private GuessService guessService;

	@Autowired
	private GuessItemService guessItemService;

	@Autowired
	private GuessLogService guessLogService;
	
	@GetMapping()
	String Guess(){
	    return "system/guess/guess";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<GuessDO> guessList = guessService.list(query);
		int total = guessService.count(query);
		PageUtils pageUtils = new PageUtils(guessList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "system/guess/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		GuessDO guess = guessService.get(id);
		model.addAttribute("guess", guess);
	    return "system/guess/edit";
	}

	@GetMapping("/detail/{id}")
	String detail(@PathVariable("id") Long id,Model model){
		GuessDO guess = guessService.get(id);
		GuessItemDO guessItemDO = new GuessItemDO();
		if(null != guess && null != guess.getAnswerId()) {
			guessItemDO = guessItemService.get(guess.getAnswerId());
		}
		model.addAttribute("guessItemDO", guessItemDO);
		model.addAttribute("guess", guess);
		return "system/guess/detail";
	}

	@GetMapping("/setAnswer/{id}")
	String setAnswer(@PathVariable("id") Long id,Model model){
		GuessDO guess = guessService.get(id);
		model.addAttribute("guess", guess);
		return "system/guess/setAnswer";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( GuessDO guess){
		if(CollectionUtils.isEmpty(guess.getGuessItemDOList())) {
			return R.error("请添加竞猜项目");
		}
		guess.setCreateBy(getUserId());
		R r = guessService.save(guess);
		return r;
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( GuessDO guess){
		if(CollectionUtils.isEmpty(guess.getGuessItemDOList())) {
			return R.error("请添加竞猜项目");
		}
		R r = guessService.update(guess);
		return r;
	}

	/**
	 * 修改状态
	 */
	@ResponseBody
	@RequestMapping("/updateState")
	public R updateState(GuessDO guess){
		if(StringUtils.equals("2",guess.getGuessState())) {
			GuessDO guessDO = guessService.get(guess.getId());
			if(null != guessDO) {
				if(new Date().getTime() >= guessDO.getGuessEndDate().getTime()) {
					return R.error("竞猜结束时间小于当前时间，不能开局");
				}
			}
		}
		R r = guessService.updateState(guess);
		return r;
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long id){
		if(guessService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		guessService.batchRemove(ids);
		return R.ok();
	}

	/**
	 * 设置密码
	 */
	@PostMapping( "/setAnswer")
	@ResponseBody
	public R setAnswer(GuessDO guessDO){
		guessService.setAnswer(guessDO);
		return R.ok();
	}

	/**
	 * 获取竞猜项目类别数据
	 * @param guessItemDO
	 * @return
	 */
	@ResponseBody
	@PostMapping("/loadGuessItem")
	public R loadGuessItem(GuessItemDO guessItemDO){
		//查询列表数据
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("guessId",guessItemDO.getGuessId());
		List<GuessItemDO> guessList = guessItemService.list(map);
		return R.ok(guessList);
	}

	@GetMapping("/guessLog")
	String GuessLog(){
		return "system/guessLog/log";
	}

	@ResponseBody
	@GetMapping("/guessLogList")
	public PageUtils guessLogList(@RequestParam Map<String, Object> params){
		//获取用户角色
		List<Long> list = userRoleDao.listRoleId(getUserId());
		if(!CollectionUtils.isEmpty(list)) {
			if(!list.contains(1l)) {
				//普通用户
				params.put("createBy",getUserId());
			}
		}
		//查询列表数据
		Query query = new Query(params);
		List<GuessLogDO> guessList = guessLogService.list(query);
		int total = guessLogService.count(query);
		PageUtils pageUtils = new PageUtils(guessList, total);
		return pageUtils;
	}
	
}
